clear all
cap log close


sysdir set PERSONAL "W:\ado"
set scheme kailascheme


*Defome dummie used in the eventstudies
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
*define fixed effects 
global fe = "baseid time year age_time match_id1" 

*define clustering
global cluster = "shnro"


capture program drop eventStudyGraphs
program define eventStudyGraphs
	args a b c d
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace coef_est = 0 if t == - 1
	replace se_est = 0 if t == -1
	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	


	
	*Main regression for top guys 
	reghdfe `a'  treatPost ,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time since Violence") ), ///
		 legend( order( 2 "Victims" ) rows(2) position(7) ring(0) ) ///
			

	* Save estimates 
	keep coef* se_est uC* lC* obs* t
	drop if missing(t)
	gen outcome = "`a'"
	restore
	}
end


use "$dataout\victim_altmatches_allyears_matchpast5_shortmatch.dta", clear

destring suspect_sex, replace 
destring plaintiff_sex, replace
	
gen mm=suspect_sex==1 & plaintiff_sex==1
gen mf=suspect_sex==1 & plaintiff_sex==2
	
sort match_id1 year_event time shnro 
bys match_id1 year_event time: ereplace mf=max(mf)
bys match_id1 year_event time: ereplace mm=max(mm)

gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

gen age_time=age+time


*Time displacement dummies
g dpl_5=time==-5 & treat==1
g dpl_4=time==-4 & treat==1
g dpl_3=time==-3 & treat==1
g dpl_2=time==-2 & treat==1
g dpl_1=time==-1 & treat==1
g dpl_0=time==0 & treat==1
gen dpl1=time==1 & treat==1
gen dpl2=time==2 & treat==1
gen dpl3=time==3 & treat==1
gen dpl4=time==4 & treat==1
gen dpl5=time==5 & treat==1

gen treatPost= treat==1 & time>0



* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)


destring suspect_sex, replace 
destring plaintiff_sex, replace 
sort match_id1 time shnro 
bys match_id1 time: ereplace suspect_sex=max(suspect_sex)
bys match_id1 time: ereplace plaintiff_sex=max(plaintiff_sex)

sort shnro year_event time 
gen prior_earnings1=realAllEarn if time==-1 
gen prior_earnings2=realAllEarn if time==-2 
gen prior_earnings3=realAllEarn if time==-3

bysort shnro year_event: ereplace  prior_earnings1=mean( prior_earnings1)
bysort shnro year_event: ereplace  prior_earnings2=mean( prior_earnings2)
bysort shnro year_event: ereplace  prior_earnings3=mean( prior_earnings3)

egen prior_earnings=rmean (prior_earnings1 prior_earnings2 prior_earnings3)

gen share_earning=realAllEarn/prior_earnings

sum share_earning, d 
replace share_earning=10 if share_earning>10

g victim=1
tempfile victim_short
save `victim_short'


// Suspect match 
use "$dataout\suspect_altmatches_allyears_matchpast5_shortmatch.dta", clear


destring suspect_sex, replace 
destring plaintiff_sex, replace
	
gen mm=suspect_sex==1 & plaintiff_sex==1
gen mf=suspect_sex==1 & plaintiff_sex==2
	
sort match_id1 year_event time shnro 
bys match_id1 year_event time: ereplace mf=max(mf)
bys match_id1 year_event time: ereplace mm=max(mm)

gen baseyr=year_event

assert time >= -5 & time <= 5 

gen treat= wp_crime==1

gen age_time=age+time


*Time displacement dummies
g dpl_5=time==-5 & treat==1
g dpl_4=time==-4 & treat==1
g dpl_3=time==-3 & treat==1
g dpl_2=time==-2 & treat==1
g dpl_1=time==-1 & treat==1
g dpl_0=time==0 & treat==1
gen dpl1=time==1 & treat==1
gen dpl2=time==2 & treat==1
gen dpl3=time==3 & treat==1
gen dpl4=time==4 & treat==1
gen dpl5=time==5 & treat==1

gen treatPost= treat==1 & time>0



* Generate treatment wave indentifier for each individual
egen baseid=group(shnro baseyr)


destring suspect_sex, replace 
destring plaintiff_sex, replace 
sort match_id1 time shnro 
bys match_id1 time: ereplace suspect_sex=max(suspect_sex)
bys match_id1 time: ereplace plaintiff_sex=max(plaintiff_sex)

sort shnro year_event time 
gen prior_earnings1=realAllEarn if time==-1 
gen prior_earnings2=realAllEarn if time==-2 
gen prior_earnings3=realAllEarn if time==-3

bysort shnro year_event: ereplace  prior_earnings1=mean( prior_earnings1)
bysort shnro year_event: ereplace  prior_earnings2=mean( prior_earnings2)
bysort shnro year_event: ereplace  prior_earnings3=mean( prior_earnings3)

egen prior_earnings=rmean (prior_earnings1 prior_earnings2 prior_earnings3)

gen share_earning=realAllEarn/prior_earnings

sum share_earning, d 
replace share_earning=10 if share_earning>10

g victim=0

tempfile suspect_short 
save `suspect_short'

// Analysis
use `victim_short'
append using `suspect_short'

label var treatPost "Treatment"

reghdfe employed  treatPost if mf==1 & victim==1,  absorb($fe)  cluster($cluster)
est sto victim_emp_mf_s
reghdfe employed  treatPost if mf==1 & victim==0,  absorb($fe)  cluster($cluster)
est sto suspect_emp_mf_s


coefplot victim_emp_mf_s suspect_emp_mf_s, keep(treatPost) ///
	vertical yline(0) ytitle("Coefficient")  ///
	legend(order(2 "Victim" 4 "Suspect")) ///
	yscale(r(-0.05 0.05)) ylabel(-0.05(0.025)0.05) name(short_mf, replace) ///
	recast(bar) barwidth(0.3) fcolor(*0.8) ciopts(recast(rcap)) citop title(Male-Female)


reghdfe employed  treatPost if mm==1 & victim==1,  absorb($fe)  cluster($cluster)
est sto victim_emp_mm_s
reghdfe employed  treatPost if mm==1 & victim==0,  absorb($fe)  cluster($cluster)
est sto suspect_emp_mm_s

// Figure 5 in the QJE 
coefplot victim_emp_mm_s suspect_emp_mm_s, keep(treatPost) ///
	vertical yline(0) ytitle("Coefficient")  ///
	legend(order(2 "Victim" 4 "Suspect")) ///
	yscale(r(-0.05 0.05)) ylabel(-0.05(0.025)0.05) name(short_mm, replace) ///
	recast(bar) barwidth(0.3) fcolor(*0.8) ciopts(recast(rcap)) citop title(Male-Male)	

	
graph combine short_mf short_mm, rows(1)
graph export "$results/figure_altdiff_short_emp.pdf", replace

	
preserve 
set scheme s1mono

reghdfe employed  treatPost if mf==1 & victim==1,  absorb($fe)  cluster($cluster)
est sto victim_emp_mf_s
reghdfe employed  treatPost if mf==1 & victim==0,  absorb($fe)  cluster($cluster)
est sto suspect_emp_mf_s


coefplot victim_emp_mf_s suspect_emp_mf_s, keep(treatPost) ///
	vertical yline(0) ytitle("Coefficient")  ///
	legend(order(2 "Victim" 4 "Suspect")) ///
	yscale(r(-0.05 0.05)) ylabel(-0.05(0.025)0.05) name(short_mf, replace) ///
	recast(bar) barwidth(0.3) fcolor(*0.8) ciopts(recast(rcap)) citop title(Male-Female)


reghdfe employed  treatPost if mm==1 & victim==1,  absorb($fe)  cluster($cluster)
est sto victim_emp_mm_s
reghdfe employed  treatPost if mm==1 & victim==0,  absorb($fe)  cluster($cluster)
est sto suspect_emp_mm_s

// Figure 5 in the QJE 
coefplot victim_emp_mm_s suspect_emp_mm_s, keep(treatPost) ///
	vertical yline(0) ytitle("Coefficient")  ///
	legend(order(2 "Victim" 4 "Suspect")) ///
	yscale(r(-0.05 0.05)) ylabel(-0.05(0.025)0.05) name(short_mm, replace) ///
	recast(bar) barwidth(0.3) fcolor(*0.8) ciopts(recast(rcap)) citop title(Male-Male)	

	
graph combine short_mf short_mm, rows(1)
graph export "$results/figure_altdiff_short_emp_bw.pdf", replace
graph export "$results/figure_altdiff_short_emp.eps", replace
restore 
